var webpack = require('webpack');
var HtmlWebPackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
module.exports = (webpackConfig) => {
  // FilenameHash
  webpackConfig.output.chunkFilename = '[name].[hash].js' // http://webpack.github.io/docs/configuration.html#output-chunkfilename

  // ClassnameHash
  const cssLoaderOption = {
    importLoaders: 1,
    modules: true,
    localIdentName: '[hash:base64:5]',
  }
  const cssLoaders = webpackConfig.module.loaders[3].loader.split('!')
  webpackConfig.module.loaders[3].loader = cssLoaders.map(item => {
    if (item.startsWith('css')) {
      return `css?${JSON.stringify(cssLoaderOption)}`
    }
    return item
  }).join('!')

  webpackConfig.resolve.alias = {
    components: `${__dirname}/src/components`,
    utils: `${__dirname}/src/utils`,
    config: `${__dirname}/src/utils/config`,
    enums: `${__dirname}/src/utils/enums`,
    src: `${__dirname}/src`,
  }

  webpackConfig.resolve.extensions = ['', '.js', '.jsx', '.scss']
  webpackConfig.resolve.modulesDirectories = ['node_modules']

/*  webpackConfig.plugins.push(new webpack.optimize.CommonsChunkPlugin({
    name: 'vendors'
  }));*/

  webpackConfig.plugins.push(
    new webpack.optimize.DedupePlugin()
  );

  webpackConfig.plugins.push(
    new webpack.optimize.OccurenceOrderPlugin());

  webpackConfig.plugins.push(
    new webpack.NoErrorsPlugin()
  );

  webpackConfig.plugins.push(new ExtractTextPlugin('css/[name].[chunkhash:8].css'));

  webpackConfig.plugins.push(
    new webpack.DefinePlugin({
      __DEBUG__: true
    }));

  // PreLoaders
  // webpackConfig.module.preLoaders = [{
  //   test: /\.js$/,
  //   enforce: 'pre',
  //   loader: 'eslint',
  // }]

/* webpackConfig.module.loaders.push({
    test: /\.scss$/,
   loader: ExtractTextPlugin.extract("style-loader", 'css-loader!sass'),
    exclude: 'node_modules'
  });*/

  /*webpackConfig.module.loaders.push({
    test: /\.scss$/i,
    loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localI‌​dentName=[name]__[local]___[hash:base64:5]!sass?sourceMap')
  });

  webpackConfig.module.loaders.push({
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    loader: 'babel',
    query: {
      presets: ['react', 'es2015', 'stage-0', 'react-hmre'],
      plugins: ['add-module-exports', "transform-runtime"]
    }
  });*/

 /* webpackConfig.module.loaders = [
    {
      test: /\.scss$/,
      loader: 'style-loader!css-loader?-minimize!autoprefixer?{browsers:["last 2 version", "> 1%", "iOS 7"]}!sass?sourceMap'
    }, {
      test: /\.less$/,
      loader: 'style-loader!css-loader?-minimize!autoprefixer?{browsers:["last 2 version", "> 1%", "iOS 7"]}!sass?sourceMap'
    },
    // { test: /\.scss$/i, loader: ExtractTextPlugin.extract('style','css?sourceMap&modules&importLoaders=1&localI‌​dentName=[name]__[local]___[hash:base64:5]!sass?sourceMap') },
    {
      test: /\.(woff|woff2|ttf|eot|svg)$/,
      loader: 'file-loader?name=fonts/[name].[hash:8].[ext]'
    },
    {
      test: /\.(png|jpg|jpeg|gif)$/,
      loader: 'url-loader?limit=8192&name=images/[name].[hash:8].[ext]'
    },{
      test: /\.(js|jsx)$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-0', 'react-hmre'],
        plugins: ['add-module-exports',"transform-runtime"]
      }
    }
  ]*/

  return webpackConfig
}
